package org.ssp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.ssp.dao.AssistDao;
import org.ssp.dao.UserDao;
import org.ssp.impl.AssistImpl;
import org.ssp.impl.UserImpl;
import org.ssp.pojo.Assister;
import org.ssp.pojo.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


/**
 * assist handle
 */
@WebServlet("/assist")
public class AssistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method=request.getParameter("method");
		if(request.getSession().getAttribute("assist")==null){
			request.getRequestDispatcher("/WEB-INF/page/admin/adm-login.jsp").forward(request, response);
		    return;
		}
		if("list".equals(method)){//1.显示assist列表
			System.out.println("assist.........list.......");
			int page=Integer.valueOf(request.getParameter("page"));//当前页
			int rows=Integer.valueOf(request.getParameter("rows"));//每页个数
			String id=request.getParameter("id");
			ArrayList<Assister> list=new ArrayList<Assister>();
			int  total = 0;
			int offset=(page-1)*rows;
			AssistDao assistd=new AssistImpl();
			list=assistd.findAll(offset,rows);
			total=assistd.findAll();
			JSONObject json=new JSONObject();
			json.element("rows", list);
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
		}else if("add".equals(method)){//2.增加助理
			System.out.println("assist.......add......");
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String pwd=request.getParameter("password");
			System.out.println(id+";"+name+";"+pwd);
			Assister assist=new Assister();
			assist.setId(id);
			assist.setName(name);
		    assist.setPassword(pwd);
			AssistDao assistd=new AssistImpl();
			assistd.addAssister(assist);
			JSONObject json=new JSONObject();
			json.element("success", true);
			json.element("msg", "添加失败");
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("updatepwd".equals(method)){//3.修改密码
			System.out.println("assist....updatepwd.......");
			String pwd=request.getParameter("password2");
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			
			System.out.println(pwd+"........."+name);
			Assister assist=new Assister();
			assist.setId(id);
			assist.setPassword(pwd);
			AssistDao assistd=new AssistImpl();
			assistd.updateAssister(assist);
			JSONObject json=new JSONObject();
			json.element("success", true);
			json.element("msg", "修改密码失败");
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("update".equals(method)){//4.修改资料
			System.out.println("assist.....update.....");
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String department=request.getParameter("department");
			String email=request.getParameter("email");
			String phone=request.getParameter("phone");
			String wxid=request.getParameter("wxid");
			String wxcode=request.getParameter("wxcode");
			String pwd=request.getParameter("password");
			System.out.println(name+";"+email+";"+phone+";"+wxid+";"+wxcode+";"+pwd);
		    Assister assist=new Assister();
		    assist.setId(id);
		    assist.setName(name);
		    assist.setEmail(email);
		    assist.setPhone(phone);
		    assist.setWxid(wxid);
		    assist.setWxcode(wxcode);
		    assist.setDepartment(department);
		    AssistDao assistd=new AssistImpl();
		    assistd.updateAssister2(assist);
		    JSONObject json=new JSONObject();
			json.element("success", true);
			json.element("msg", "修改资料失败");
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("noassignlist".equals(method)){//5,获取还没有助理的用户
			int page=Integer.valueOf(request.getParameter("page"));//当前页
			int rows=Integer.valueOf(request.getParameter("rows"));//每页条数
			System.out.println("noassignlist........."+page+";"+rows);
			int offset=(page-1)*rows;
			int total=0;
			UserDao userd=new UserImpl();
			ArrayList<User> list=userd.findNoassigned(offset,rows);
			total=userd.findAllNoAssign();
			//json-lib
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
		}else if("dispatch".equals(method)){//6.分配用户
			System.out.println("assist...dispatch.......");
			String id=request.getParameter("id");
			String ids=request.getParameter("ids");
			String[] uids=ids.split(",");
			for(int i=0;i<uids.length;i++){
				User user=new User();
				user.setUid(uids[i]);
				user.setAssistid(id);
				UserDao userd=new UserImpl();
				userd.updateAssistid(user);
			}
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
		
		}else if("hasuser".equals(method)){//7.获取某个助理负责的用户列表
			System.out.println("assist........hasuser..");
			String id=request.getParameter("id");
			int page=Integer.valueOf(request.getParameter("page"));//当前页
			int rows=Integer.valueOf(request.getParameter("rows"));//每页条数
			int offset=(page-1)*rows;
			int total=0;
			UserDao userd=new UserImpl();
			ArrayList<User> list=userd.findByAssistid(id,offset,rows);
			total=userd.findByAssistid(id);
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig1 = new JsonConfig();
			jsonConfig1.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig1));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
		}else if("deleteuser".equals(method)){//8.删除助理的用户
			System.out.println("assist.......deleteuser......");
			String assistid=request.getParameter("id");
			String ids=request.getParameter("ids");
			String[] uids=ids.split(",");
			for(int i=0;i<uids.length;i++){
				User user=new User();
				user.setUid(uids[i]);
				user.setAssistid(null);
				UserDao userd=new UserImpl();
				userd.updateAssistid(user);
			}
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("deleteassist".equals(method)){//9.删除助理
			System.out.println("assist.....deleteassist.....");
			String id=request.getParameter("id");
			System.out.println(id);
			AssistDao assistd=new AssistImpl();
			assistd.deleteAssister(id);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else{
			request.getRequestDispatcher("/WEB-INF/page/admin/assist.jsp").forward(request, response);
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
